﻿using HoverTree.HoverTreeFrame.HvtImage;
using HoverTree.Model;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace HoverTree.BLL
{
    public class HvtPicture
    {
        static readonly IDAL.IPicture dal = HoverTree.DALFactory.DataAccess.CreatePicture();
        public string Add(PictureInfo picInfo, FileUpload fileUpload)
        {
            return Add(picInfo, fileUpload,new HvtPicConfig());
        }

        public string Add(PictureInfo picInfo, FileUpload fileUpload,HvtPicConfig config)
        {
            return dal.Add(picInfo, fileUpload,config);
        }

        public List<PictureInfo> HvtPageQuery(PictureSelect select, int sortType, int pageSize, int pageIndex, out int totalCount, out int totalPageCount)
        {
            return dal.HvtPageQuery(select, sortType, pageSize, pageIndex, out totalCount, out totalPageCount);
        }

        public PictureInfo Get(PictureSelect select)
        {
            return dal.Get(select);
        }

        public string ReUpload(PictureInfo info, FileUpload fileUpload)
        {
            return ReUpload(info, fileUpload,new HvtPicConfig ());
        }

        public string ReUpload(PictureInfo info, FileUpload fileUpload,HvtPicConfig config)
        {
            return dal.ReUpload(info, fileUpload,config);
        }

        public string AlterInfo(PictureInfo pictureInfo)
        {
            if(pictureInfo.HvtIsCover)
            {
                if (!pictureInfo.HvtIsshow)
                    return "封面不能设置为隐藏。";
            }

            return dal.AlterInfo(pictureInfo);
        }

        public PictureInfo GetLastPublic(PictureInfo info)
        {
            return dal.GetLastPublic(info);
        }

        public PictureInfo GetNextPublic(PictureInfo info)
        { return dal.GetNextPublic(info); }

        /// <summary>
        /// 生成页面
        /// </summary>
        /// <param name="saInfo"></param>
        /// <returns></returns>
        public string Generate(PictureInfo info)
        {
            HvtPictureGenerate m_generate = new HvtPictureGenerate();

            PictureInfo lastInfo, nextInfo;
            lastInfo = dal.GetLastPublic(info);
            nextInfo = dal.GetNextPublic(info);

            return m_generate.Generate(info, lastInfo, nextInfo);
        }

        /// <summary>
        /// 生成新添加的图片，包括上下图片
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public string GenerateNewAdd(PictureInfo info)
        {
            HvtPictureGenerate m_generate = new HvtPictureGenerate();

            
            PictureInfo lastInfo, nextInfo;
            lastInfo = dal.GetLastPublic(info);
            nextInfo = dal.GetNextPublic(info);

            string m_tips = "";
            //生成刚添加图片
            m_tips= m_generate.Generate(info, lastInfo, nextInfo);
            if (m_tips != string.Empty)
                return " 新添加的图片无法生成。"+m_tips;

            //生成上一个
            if (lastInfo != null)
            {
                m_tips = m_tips + m_generate.Generate(lastInfo, dal.GetLastPublic(lastInfo), info);
                if (m_tips != string.Empty)
                {
                    return "上一个图片无法生成。" + m_tips;
                }
            }

            //生成下一个
            if (nextInfo != null)
            {
                m_tips = m_tips + m_generate.Generate(nextInfo, info, dal.GetNextPublic(nextInfo));

                if (m_tips != string.Empty)
                {
                    return "下一个图片无法生成。" + m_tips;
                }
            }
                return m_tips;
        }

        public string SetCover(PictureSelect select)
        {
            return dal.SetCover(select);
        }

        public List<PictureInfo> GetListWithImageSetTitle(PictureSelect pictureSelect, int sortType, int pageSize, int pageIndex, out int totalCount, out int totalPageCount)
        {
            return dal.GetListWithImageSetTitle(pictureSelect, sortType, pageSize, pageIndex, out totalCount, out totalPageCount);
        }

        public string GenerateAll()
        {
            return GenerateAll(-1);
        }

        public string GenerateAll(int imageSetId)
        {
            List<PictureInfo> m_list = new List<PictureInfo>();

            m_list = dal.GetAllPublic(imageSetId);

            int m_count = m_list.Count;
            if (m_count < 1)
                return "没有题目";
            string m_result;
            foreach (PictureInfo info in m_list)
            {
                m_result = Generate(info);
                if (m_result != string.Empty)
                    return m_result;
            }
            return "";
        }
    }
}